<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      padding:0;
      margin: 0;
    }
    .alertBox{
      width: 300px;
      height: 300px;
      background: #ccc;
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -150px 0 0 -150px;
      border: 1px solid #000;
    }
    .title{
      width: 100%;
      height: 50px;
      line-height: 50px;
      background: #999;
      color:red;
      text-align: center;
      font-size: 20px;
      font-weight: bold;
    }
    .content{
      height:250px;
      line-height: 250px;
      background: green;
      color: yellow;
      text-align: center;
      font-size: 25px;
    }
  </style>
  <script>
    class box{
      //构造体 雏形
        constructor(title='我是标题',content='我是内容',type = '1'){
          this.alertBox = document.createElement('div');
          this.alertBox.name = 'yes';
          this.alertBox.classList.add('alertBox');
          this.alertBox.classList.add('type' + type);
          this.titleEle = document.createElement('h2');
          this.titleEle.className = 'title';
          this.titleEle.innerHTML = title;
          this.contentEle = document.createElement('div');
          this.contentEle.className = 'content';
          this.contentEle.innerHTML = content;
          this.alertBox.appendChild(this.titleEle);
          this.alertBox.appendChild(this.contentEle);
          this.alertWin();
        }
        // 这个方法就是创建出box
        showWin(){
          document.querySelector('body').appendChild(this.alertBox);
          this.inputX = document.createElement('input');
          this.inputX.classList.add('x');
          this.inputX.value ='消失';
          this.inputX.type = 'button';
          document.querySelector('body').appendChild(this.inputX);
          this.bugAlert();
        }
        // 给目标进行绑定
        alertWin(){
          document.querySelector('.ok').addEventListener('click',()=>{
            if(this.alertBox.name == 'no') return;
            this.alertBox.name = 'no';
            this.showWin();
          },false);
        }
        // 给取消按钮进行绑定
        bugAlert(){
          document.querySelector('.x').addEventListener('click',()=>{
            document.querySelector('body').removeChild(this.alertBox);
            document.querySelector('body').removeChild(this.inputX);
            this.alertBox.name = 'yes';
          },false);
        }
    }
  </script>
  <script>
  document.addEventListener('DOMContentLoaded',function (){
    new box('我就是个标题而已','那么我也只能是内容了','1');
  },false);
  </script>
</head>
<body>
  <input type="button" value="按钮" class="ok">
</body>
</html>
